import IndexAdUpdate from '../index-ad-update/index-ad-update.vue'
import Pages from '../../pages/pages.vue'
import { getIndexAdList, deleteIndexAd, showMsg } from '../../../vuex/action'

export default {
  name: 'IndexList',
  components: {
    'vue-index-ad-update': IndexAdUpdate,
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  ready() {
    this.getIndexList({})
  },
  vuex: {
    getters: {
      fresh: state => state.indexAdListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetIndexAdList: getIndexAdList,
      xDeleteIndexAd: deleteIndexAd
    }
  },
  methods: {
    deleteIndexAd(id) {
      this.xDeleteIndexAd(id)
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    getIndexList({ start = this.pages.start, limit = this.pages.limit }) {
      this.xGetIndexAdList({
          start: start,
          limit: limit
        })
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getIndexList({})
      }
    }
  }
}
